package com.qianchang.microfinance.view.credit.data;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.BitmapUtils;
import com.dm.library.utils.ToastUtil;
import com.lzy.imagepicker.bean.ImageItem;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.UploadFileBean;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.manager.HImageUserChoseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HandIdentityCarActivity extends HImageUserChoseActivity implements HImageUserChoseActivity.UpLoadFileCallback {
    @BindView(R.id.btn_img)
    ImageView btn_img;

    @BindView(R.id.alf_btn)
    Button alf_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hand_identity_car);
        ButterKnife.bind(this);
        setTitle("手持身份证上传");
        setUpLoadCallBackListener(this);
    }
    @OnClick({R.id.btn_img,R.id.alf_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_img: //头像
                showSelectPhotoDialog();

                break;

            case R.id.alf_btn:
                finish();
                break;
        }
    }
    @Override
    public void onPhotoResult(ArrayList<ImageItem> imageItems, int flag) {
        if (null != imageItems && imageItems.size() > 0) {
            String filePath = imageItems.get(0).path;
            String file = BitmapUtils.createFile(this, filePath);
            uploadFile(file);
            clearSelectImages();
        }
    }

    //上传头像成功
    @Override
    public void onUpLoadSuccess(UploadFileBean uploadFileBean, String imagePath, int flag) {
        if (null != uploadFileBean) {
            String imageUrl = uploadFileBean.getUrl();
            //更新用户头像
            updateUserPhoto(imageUrl, imagePath);
        }
    }

    //提交上传的头像url
    private void updateUserPhoto(final String imageUrl, final String imagePath) {
        HttpParams params = new HttpParams();
        params.put("imageUrl", imageUrl);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.UPDATE_HANDHELDIDCARD_PHOTO, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    // Glide.with(UserInfoActivity.this).load(imagePath).error(R.drawable.img_swed).into(ivHeadImg);                //2017.12.14 注释掉迪蒙的
                    Glide.with(HandIdentityCarActivity.this).load(imagePath).error(R.drawable.img_swed).into(btn_img);                //2017.12.14 鸿胜科技新增自己的
                    alf_btn.setEnabled(true);
                    alf_btn.setBackground(getResources().getDrawable(R.drawable.btn_refinance_confirm));

                } else {
                    if (!"300001".equals(response.getCode())) {
                        if (!"300084".equals(response.getCode())) {
                            ToastUtil.getInstant().show(HandIdentityCarActivity.this, response.getDescription());
                        }
                    } else {
                        DMApplication.getInstance().setUserLoginInfo(null);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                ToastUtil.getInstant().show(HandIdentityCarActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

}
